package edu.zut.cs.software.example.square.web.spring.controller;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import edu.zut.cs.software.example.QueryInfo.QueryInfo;
import edu.zut.cs.software.example.square.dao.SquareDao;
import edu.zut.cs.software.example.square.domain.Square;
import edu.zut.cs.software.example.square.service.SquareManager;

/**
 * 
 * @author zhangjiahua 
 *
 */

@RestController
public class SquareController {

	@Autowired
	SquareManager squareManager;

	@Autowired
	SquareDao squareDao;
	/**
	 * 默认查询该用户的所有tiezi
	 * 
	 * @param queryInfo
	 * @return
	 */
	@RequestMapping("/square")
	public String getSquareList(QueryInfo queryInfo) {
		int numbers = squareManager.getSquareCounts();

		int pagenum = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
//		System.out.println("pageStart:"+pageStart);

		List<Square> Square = squareManager.getBySquare(pagenum, queryInfo.getPageSize());
//		System.out.println("查询结果列表："+Space);
		HashMap<String, Object> res = new HashMap<>();
		res.put("numbers", numbers);
		res.put("data", Square);

		String res_string = JSON.toJSONString(res);

		return res_string;

	}


	/**
	 * 添加学习笔记
	 * 
	 * @param space
	 * @return
	 */
	@RequestMapping("/addsquare")
	public String addsquare(@RequestBody Square square) {// 传来的数据转换为对象json->obj
		Square addsqu = squareManager.save(square);
		return null;
	}

	/**
	 * 删除学习笔记
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/deletesquare")
	public String deletesquare(int id) {
		//System.out.println("*****ID******:" + id);
		int i = squareManager.deleteById(id);
		//System.out.println("*****result******:" + i);
		String str = i >= 0 ? "success" : "error";
		System.out.println("*****result******:" + str);
		return str;
	}

	/**
	 * 修改学习笔记
	 * 
	 * @param space
	 * @return
	 */

	@RequestMapping("/editsquare")
	public String editsquare(@RequestBody Square square) {
		String flag = "error";

		//System.out.println(square);
		
		int squ = squareManager.editsquare(square);
		
		// System.out.println(squ);
		 
		if (squ == 1)
			flag = "success";
		HashMap<String, Object> res = new HashMap<String, Object>();
		res.put("flag", flag);

		int id = Integer.parseInt(String.valueOf(square.getId()));

		Square newSquInfo = squareManager.getUpdateSquService(id);

		res.put("square", newSquInfo);

		String r = JSON.toJSONString(res);

		return r;
	}

	/**
	 * 按标题查找
	 * 
	 * @param queryInfo
	 * @return
	 */
	@RequestMapping("/getBynames")
	public String getSquareListByName(QueryInfo queryInfo) {
//			System.out.println("前端携带数据： "+queryInfo+" 请求/getByState");
		int numbers = squareManager.getSquareBytitleCounts(queryInfo.getQuery());
//			System.out.println("numbers:"+numbers);

		int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
//			System.out.println("pageStart:"+pageStart);

		List<Square> Square = squareManager.findBytitle(queryInfo.getQuery(), pageStart, queryInfo.getPageSize());
		//System.out.println("查询结果列表：" + Square);

		HashMap<String, Object> res = new HashMap<>();
		res.put("numbers", numbers);
		res.put("data", Square);

		String res_string = JSON.toJSONString(res);

		return res_string;

	}

	/**
	 * 查找修改的数据
	 * 
	 * @param square
	 * @return
	 */
	@RequestMapping("/getupdatesquare")
	public String getUpdateSquare(int id) {
		// System.out.println(id);
		String squ = JSON.toJSONString(squareManager.getUpdateSquService(id));
		// System.out.println(squ);
		return squ;
	}
}
